/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Control.ControlItem;
import Control.ControlModulo;
import java.awt.JobAttributes;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author DIEGOALV
 */
public class frmMantenerItem extends javax.swing.JFrame {
    //ImageIcon etiquetaImagen;
    ArrayList<ArrayList> listadoCombo =new ArrayList<ArrayList>();
    ArrayList<ArrayList> listadoItems =new ArrayList<ArrayList>();
    
    public frmMantenerItem() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/Media/logo1.png")).getImage());
        cargarComboModulo();
        cargarTablaItems();
        
    }
    
    
    public void cargarComboModulo(){
        ControlModulo control=new ControlModulo();
         listadoCombo = control.listarTodos();
        
        for (int i = 0; i < listadoCombo.size(); i++) {
            System.out.println("Codigo mod: "+listadoCombo.get(i).get(0));
            comboModulo.addItem(listadoCombo.get(i).get(1));
            
        }
     
    }
    
    public void cargarTablaItems(){
        ControlItem control=new ControlItem();
        listadoItems = control.listarTodos();
        for (int i = 0; i < listadoItems.size(); i++) {
            tblVistas.setValueAt(listadoItems.get(i).get(0),i,0);
            tblVistas.setValueAt(listadoItems.get(i).get(1), i, 1);
            tblVistas.setValueAt(listadoItems.get(i).get(2), i, 2);
            tblVistas.setValueAt(listadoItems.get(i).get(3), i, 3);
            tblVistas.setValueAt(listadoItems.get(i).get(4), i, 4);
            
        }
        
        
    }
    
    
    public void limpiarCampos(){
        txtNombreArchivo.setText("");
        txtNombreOpcion.setText("");
        txtDescripcion.setText("");
        comboModulo.setSelectedIndex(0);
    }

    
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtNombreOpcion = new javax.swing.JTextField();
        btnRegistrar = new javax.swing.JButton();
        txtNombreArchivo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        comboModulo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblVistas = new javax.swing.JTable();
        btnSeleccionar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 240));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Mantenimiento de vistas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 24), new java.awt.Color(255, 255, 153))); // NOI18N

        btnRegistrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Media/check.png"))); // NOI18N
        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre opción:");

        jLabel2.setText("Nombre archivo .java:");

        jLabel3.setText("Módulo:");

        jLabel4.setText("Descripción de la vista:");

        txtDescripcion.setColumns(20);
        txtDescripcion.setRows(5);
        jScrollPane1.setViewportView(txtDescripcion);

        jLabel5.setText("(Máx.250 carácteres)");

        jPanel2.setBackground(new java.awt.Color(204, 204, 240));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Vistas registradas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 153))); // NOI18N

        tblVistas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "ID", "Modulo", "Nombre opcion", "Nombre archivo", "Descripcion"
            }
        ));
        jScrollPane2.setViewportView(tblVistas);

        btnSeleccionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Media/edit_reserva.png"))); // NOI18N
        btnSeleccionar.setText("Modificar");
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Media/cancel_reserva.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(btnSeleccionar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSeleccionar)
                    .addComponent(btnEliminar))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRegistrar)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboModulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNombreArchivo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombreOpcion, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(comboModulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtNombreOpcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombreArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(btnRegistrar)))
                .addContainerGap(82, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        if (btnRegistrar.getText().equals("Registrar")){
            
            
        
        ControlItem control = new ControlItem();
        
        //JOptionPane.showMessageDialog(this, comboModulo.getSelectedIndex());
        int idModulo = Integer.parseInt(listadoCombo.get(comboModulo.getSelectedIndex()).get(0).toString());
        //JOptionPane.showMessageDialog(this, idModulo);
       
        boolean inserto = control.agregarItem(idModulo, txtNombreOpcion.getText(), txtNombreArchivo.getText(), txtDescripcion.getText());
        
            if (inserto){
                JOptionPane.showMessageDialog(this, "Se registró el item correctamente", "Registro completo!", JOptionPane.INFORMATION_MESSAGE);   
            }else{
                JOptionPane.showMessageDialog(this, "Error al registrar", "Error!", JOptionPane.ERROR_MESSAGE);
            }
            
            limpiarCampos();
            cargarTablaItems();
        }else {
           ControlItem control = new ControlItem(); 
           int idModulo = Integer.parseInt(listadoCombo.get(comboModulo.getSelectedIndex()).get(0).toString());
           int id       = Integer.parseInt(tblVistas.getValueAt(tblVistas.getSelectedRow(), 0).toString());
           
           
           boolean modifico = control.editarItem(id,idModulo,txtNombreOpcion.getText(),txtNombreArchivo.getText(),txtDescripcion.getText());
           
            if(modifico){
            JOptionPane.showMessageDialog(this, "Se modificó el item correctamente", "Modificación completa!", JOptionPane.INFORMATION_MESSAGE);   
            limpiarCampos();
            cargarTablaItems();
            btnRegistrar.setText("Registrar");
            }else {
            JOptionPane.showMessageDialog(this, "Error al actualizar", "Error!", JOptionPane.ERROR_MESSAGE);   
          }
        
        }    
        
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        limpiarCampos();
        comboModulo.setSelectedItem(""+tblVistas.getValueAt(tblVistas.getSelectedRow(), 1));
        txtNombreOpcion.setText(""+tblVistas.getValueAt(tblVistas.getSelectedRow(), 2));
        txtNombreArchivo.setText(""+tblVistas.getValueAt(tblVistas.getSelectedRow(), 3));
        txtDescripcion.setText(""+tblVistas.getValueAt(tblVistas.getSelectedRow(), 4));
        btnRegistrar.setText("Confirmar");
    }//GEN-LAST:event_btnSeleccionarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
      
        String nombreVista = tblVistas.getValueAt(tblVistas.getSelectedRow(), 2).toString();
        
      int respuesta =  JOptionPane.showConfirmDialog(this, "Desea eliminar la vista "+nombreVista, "Confirme", JOptionPane.YES_NO_OPTION);
       
      if (respuesta == 0){
      
        ControlItem control = new ControlItem();
      boolean elimino = control.eliminarItem(Integer.parseInt(tblVistas.getValueAt(tblVistas.getSelectedRow(), 0).toString()));
      
        if (elimino){
            JOptionPane.showMessageDialog(this, "Se elimino correctamente", "Eliminación completa!", JOptionPane.INFORMATION_MESSAGE);            
            cargarTablaItems();     
        }else{
            JOptionPane.showMessageDialog(this, "Se produjo un error al eliminar", "Error!", JOptionPane.ERROR_MESSAGE);
        }     
      }
      
      
    }//GEN-LAST:event_btnEliminarActionPerformed

    public void limpiarTabla(){
        
    }
    
    
    
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmMantenerItem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmMantenerItem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmMantenerItem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmMantenerItem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmMantenerItem().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JComboBox comboModulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblVistas;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtNombreArchivo;
    private javax.swing.JTextField txtNombreOpcion;
    // End of variables declaration//GEN-END:variables
}
